package com.muatocroi.tornado;

import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.*;

public class ViewTouchPad extends View {
	public Rect rectLButton = new Rect();
	public Rect rectRButton = new Rect();
	public Rect rectMove = new Rect();
	public RectF rectScroll = new RectF();

	public boolean isScrolling = false;
	public boolean isLeftDowning = false;
	public boolean isRightDowning = false;

	private int HeightMove = 0, WidthMove = 0;

	private RectF rectView = new RectF();
	private RectF rectScrollPaint = new RectF();
	private Paint paintButtonDown = new Paint();
	private Paint paintBg = new Paint();
	private Paint paintPath = new Paint();
	private Paint paintPathScrollSTROKE = new Paint();
	private Paint paintPathScrollFILL = new Paint();

	public ViewTouchPad(Context context, AttributeSet attrs) {
		super(context, attrs);

		paintBg.setColor(Color.BLACK);
		paintPath.setColor(Color.LTGRAY);
		paintPath.setStyle(Paint.Style.STROKE);
		paintPath.setStrokeWidth(5);

		paintPathScrollSTROKE.setColor(Color.LTGRAY);
		paintPathScrollSTROKE.setStyle(Paint.Style.STROKE);
		paintPathScrollSTROKE.setStrokeWidth(3);

		paintPathScrollFILL.setColor(0x80025432);
		paintPathScrollFILL.setStyle(Paint.Style.FILL);
		paintPathScrollFILL.setStrokeWidth(3);

		paintButtonDown.setColor(0x80CCCCCC);
		paintButtonDown.setStyle(Paint.Style.FILL);
		paintButtonDown.setStrokeWidth(5);

	}


	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawRect(rectView, paintBg);
		canvas.drawRect(rectView, paintPath);
		canvas.drawRect(rectLButton, paintPath);
		canvas.drawRect(rectRButton, paintPath);
		canvas.drawRect(rectScrollPaint, paintPathScrollSTROKE);
		if (isScrolling) {
			canvas.drawRect(rectScrollPaint, paintPathScrollFILL);
		}
		if (isLeftDowning) {
			canvas.drawRect(rectLButton, paintButtonDown);
		}
		if (isRightDowning) {
			canvas.drawRect(rectRButton, paintButtonDown);
		}
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		HeightMove = (int) (h * 0.80);
		WidthMove = (int) (w * 0.87);

		rectMove.top = 1;
		rectMove.left = 1;
		rectMove.right = WidthMove;
		rectMove.bottom = HeightMove;

		rectLButton.left = 1;
		rectLButton.right = w / 2 - 1;
		rectLButton.top = HeightMove + 1;
		rectLButton.bottom = h - 5;

		rectRButton.left = w / 2 + 1;
		rectRButton.right = w - 1;
		rectRButton.top = HeightMove + 1;
		rectRButton.bottom = h - 5;
		
		rectScrollPaint.top = 20;
		rectScrollPaint.bottom = HeightMove - 20;
		rectScrollPaint.left = WidthMove + (w - WidthMove) / 2 - 3;
		rectScrollPaint.right = rectScrollPaint.left + 15;
		
		rectScroll.top = 1;
		rectScroll.bottom = HeightMove -1;
		rectScroll.left = WidthMove + 1;
		rectScroll.right = w - 1;
		
		rectView.left = 0;
		rectView.top = 0;
		rectView.right = w;
		rectView.bottom = h;
	}

}
